package besteAukera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Vector;

public class UAL extends Konbinazionalak {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Color kolorea;
	private Vector<Hanka> hankaBektorea = new Vector<Hanka>();
	private Vector<Hanka> sarreraBektorea = new Vector<Hanka>();
	private Hanka s1, s2, s4, i1, i2, s3, s5, i3, i4;

	public UAL(int x1, int y1, int hankaKop, int sarreraKopMax, String mota,
			Color kolorea) {
		super(x1, y1, hankaKop, sarreraKopMax, mota, kolorea);
		// TODO Auto-generated constructor stub
		s1 = new Hanka(x + 15, y - 20, "positiboa", "s", 0, this, "b", "ez",false);
		s2 = new Hanka(x + 64, y - 20, "positiboa", "s", 1, this, "b", "ez",false);
		s3 = new Hanka(x + 95, y + 15, "positiboa", "s", 2, this, "h", "bai",true);
		s4 = new Hanka(x + 87, y + 33, "positiboa", "s", 3, this, "h", "bai",true);
		s5 = new Hanka(x + 81, y + 50, "positiboa", "s", 4, this, "h", "bai",true);
		i1 = new Hanka(x + 40, y + 80, "positiboa", "i", 5, this, "b", "ez",false);
		i2 = new Hanka(x - 16, y + 15, "positiboa", "i", 6, this, "h", "bai",true);
		i3 = new Hanka(x - 9, y + 33, "positiboa", "i", 7, this, "h", "bai",true);
		i4 = new Hanka(x, y + 50, "positiboa", "i", 8, this, "h", "bai",true);

		lotList[0] = s1;
		lotList[1] = s2;
		lotList[2] = s3;
		lotList[3] = s4;
		lotList[4] = s5;
		lotList[5] = i1;
		lotList[6] = i2;
		lotList[7] = i3;
		lotList[8] = i4;

	}

	public void marraztu(Graphics g) {

		if (getAukeratua() == true) {
			kolorea = Color.GRAY;
		} else {
			kolorea = Color.black;
		}
		g.setColor(kolorea);
		g.drawLine(x, y, x + 30, y);
		g.drawLine(x + 50, y, x + 80, y);
		g.drawLine(x + 30, y, x + 40, y + 20);
		g.drawLine(x + 40, y + 20, x + 50, y);

		g.drawLine(x + 25, y + 60, x + 55, y + 60);
		g.drawLine(x, y, x + 25, y + 60);
		g.drawLine(x + 55, y + 60, x + 80, y);
		Font lehenetsia=g.getFont();
		Font f=new Font("name",Font.BOLD,9);
		g.setFont(f);
		g.drawString("B", x+15, y+9);
		g.drawString("A", x+60, y+9);
		g.drawString("Cin", x+57, y+17);
		g.drawString("Cout", x+8, y+17);
		g.drawString("Z", x+15, y+37);
		g.drawString("N", x+22, y+52);
		g.drawString("EK0", x+47, y+37);
		g.drawString("EK1", x+42, y+52);
		g.drawString("Y", x+37, y+59);
		g.setFont(lehenetsia);
	 	g.setColor(kolorea);
		s1.marraztu(g);
		s2.marraztu(g);
		s3.marraztu(g);
		s4.marraztu(g);
		s5.marraztu(g);
		i1.marraztu(g);
		i2.marraztu(g);
		i3.marraztu(g);
		i4.marraztu(g);
	}

	public boolean barruanDago(int x, int y) {
		if ((x > this.x) && (x < (this.x + 60)) && (y > this.y)
				&& (y < (this.y + 80))) {
			return true;
		}
		return false;
	}

	@Override
	protected void funtzioLogikoa(Integer... values) {
		// TODO Auto-generated method stub

		if ((s4.getSarrera().getBalioa() == 0)
				&& (s5.getSarrera().getBalioa() == 0)) {
			lotList[5].getIrteera().setBalioa(
					s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
							+ s3.getSarrera().getBalioa());
			if (getDatuMota().compareTo("Positiboa") == 0) {
				// carry-aren tratamendua
				if (s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa() > Math.pow(2, bitKop) - 1) {
					lotList[6].getIrteera().setBalioa(1);
				} else
					lotList[6].getIrteera().setBalioa(0);
			} else if (getDatuMota().compareTo("BirakoOs") == 0) {
				Balioa b1 = new Balioa(bitKop);
				Balioa b2 = new Balioa(bitKop);
				Balioa bi1 = new Balioa(bitKop);
				Vector<Integer> sar1 = b1.getBalioBitarra(s1.getSarrera()
						.getBalioa());
				Vector<Integer> sar2 = b2.getBalioBitarra(s2.getSarrera()
						.getBalioa());
				Vector<Integer> irt1 = bi1.getBalioBitarra(s1.getSarrera().getBalioa() + s2.getSarrera().getBalioa()
						+ s3.getSarrera().getBalioa());
				if ((sar1.get(bitKop - 1).equals(-sar2.get(bitKop - 1)))) {
					if ((sar2.get(bitKop-1)).equals(-irt1.get(bitKop - 1)))
						lotList[6].getIrteera().setBalioa(0);
					else
						lotList[6].getIrteera().setBalioa(1);

				} else
					lotList[6].getIrteera().setBalioa(0);
			}
		
		}
		if ((s4.getSarrera().getBalioa() == 0)
				&& (s5.getSarrera().getBalioa() == 1))
			lotList[5].getIrteera().setBalioa(
					s1.getSarrera().getBalioa() & s2.getSarrera().getBalioa());
		if ((s4.getSarrera().getBalioa() == 1)
				&& (s5.getSarrera().getBalioa() == 0))
			lotList[5].getIrteera().setBalioa(
					s1.getSarrera().getBalioa() | s2.getSarrera().getBalioa());
		if ((s4.getSarrera().getBalioa() == 1)
				&& (s5.getSarrera().getBalioa() == 1))
			lotList[5].getIrteera().setBalioa(~(s1.getSarrera().getBalioa()));
		
		//emaitza 0 bada Z=1 bestela Z=0
		if (lotList[5].getIrteera().getBalioa().equals(0))
			lotList[7].getIrteera().setBalioa(1);
		else lotList[7].getIrteera().setBalioa(0);
		//emaitza negatiboa bada N=1 bestela N=0
		if (lotList[5].getIrteera().getBalioa()<0)
			lotList[8].getIrteera().setBalioa(1);
		else lotList[8].getIrteera().setBalioa(0);
	}

	@Override
	public Vector<Hanka> getHankak() {
		// TODO Auto-generated method stub
		hankaBektorea = new Vector<Hanka>();
		hankaBektorea.addElement(s1);
		hankaBektorea.addElement(s2);
		hankaBektorea.addElement(s3);
		hankaBektorea.addElement(s4);
		hankaBektorea.addElement(s5);
		hankaBektorea.addElement(i1);
		hankaBektorea.addElement(i2);
		hankaBektorea.addElement(i3);
		hankaBektorea.addElement(i4);
		return hankaBektorea;
	}

	@Override
	public Vector<Hanka> getSarreraHankak() {
		// TODO Auto-generated method stub
		sarreraBektorea = new Vector<Hanka>();
		sarreraBektorea.addElement(s1);
		sarreraBektorea.addElement(s2);
		sarreraBektorea.addElement(s3);
		sarreraBektorea.addElement(s4);
		sarreraBektorea.addElement(s5);
		return sarreraBektorea;
	}

}
